package com.bsoft.rpc.basic.transport;

import com.alibaba.fastjson.JSON;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

/**
 * 服务端处理类
 */
@Slf4j
public class NettyServerHandler extends SimpleChannelInboundHandler<Message> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Message message) throws Exception {
        log.info("========> received client:[{}] msg : {}",ctx.channel().remoteAddress().toString().substring(1), JSON.toJSONString(message));
        // 响应客户端
        message.setBody("banana");
        message.setTimestamp(System.currentTimeMillis());
        message.setVersion("1.0.1");
        // 把响应内容写入到与客户端通信的channel中
        ctx.channel().writeAndFlush(message);
    }
}
